package com.cs.user.domain.service.impl;

import com.cs.user.common.entity.ErrorCode;
import com.cs.user.common.utlis.ThrowUtils;
import com.cs.user.domain.bos.PermissionBO;
import com.cs.user.domain.service.PermissionDomainService;
import com.cs.user.infra.basic.entity.Permission;
import com.cs.user.infra.basic.entity.Role;
import com.cs.user.infra.basic.service.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PermissionDomainServiceImpl implements PermissionDomainService {

    @Resource
    private PermissionService permissionService;

    @Override
    public Boolean addPermission(PermissionBO permissionBO) {
        // 参数校验
        ThrowUtils.throwIf(StringUtils.isBlank(permissionBO.getPermissionName()), ErrorCode.PARAMS_ERROR,"权限名称呢？");
        // 类型转换
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionBO, permission);
        // 调用infra层方法
        return permissionService.addPermission(permission);
    }

    @Override
    public Boolean deletePermission(PermissionBO permissionBO) {
        // 参数校验
        ThrowUtils.throwIf(permissionBO.getBusinessId() == null, ErrorCode.PARAMS_ERROR,"权限id呢？");
        // 类型转换
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionBO, permission);
        // 调用infra层方法
        return permissionService.deletePermission(permission);
    }

    @Override
    public Boolean updatePermission(PermissionBO permissionBO) {
        // 参数校验
        ThrowUtils.throwIf(permissionBO.getBusinessId() == null, ErrorCode.PARAMS_ERROR,"权限id呢？");
        // 类型转换
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionBO, permission);
        // 调用infra层方法
        return permissionService.updatePermission(permission);
    }
}
